<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- 引入样式文件 -->
        <link rel="stylesheet" href="{{ asset('css/vant.index.css') }}"/>
        <title>数治科技-客户列表</title>
        <style>
            .customers .user-cell{
                display: flex;
                padding: 0.3rem;
                border-bottom: 1px solid #ededed;
                align-items: center;
            }
            .customers .user-cell .avatar{
                width: 1rem;
                height: 1rem;
                border-radius: 50%;
                margin-right: 0.3rem;
            }
            .customers .user-cell .text{
                font-size: 0.4rem;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <ul class="customers"></ul>
        </div>
    </body>

    <!-- 引入 Vue 和 Vant 的 JS 文件 -->
    <script src="{{ asset('js/rem.js') }}"></script>
    <script src="{{ asset('js/vue.min.js') }}"></script>
    <script src="{{ asset('js/vant.min.js') }}"></script>
    <script src="{{ asset('js/jquery.min.js') }}"></script>

    <script>

        function replaceQueryString (url, name, value) {
            const re = new RegExp(name + '=[^&]*', 'gi')
            return url.replace(re, name + '=' + value)
        }

        const toast = vant.Toast.loading({
            message: '加载中...',
            forbidClick: true,
            duration: 0
        });

        let callback = '<?php echo $callback;?>';
        let chatId = '<?php echo $chatId;?>';
        let staffId = '<?php echo $contact['userid'];?>';
        let staffName = '<?php echo $contact['name'];?>';
        $(function() {
            $.ajax({
                type: "GET",
                url: "/api/transfer",
                data: {
                    callback: callback,
                    url: 'sys/thirdApp/wechatEnterprise/groupclient',
                    method: 'post',
                    payload: JSON.stringify({ chat_id: chatId, need_name: 1 })
                },
                dataType: "json",
                success: function(data) {
                    if (data.success === true) {
                        let customers = ''
                        $.each(data.result, function(index, item) {
                            customers += '<li class="user-cell" data-userid="'+item.patientId+'">' +
                                '<img class="avatar" src="'+item.avatar+'" />' +
                                '<p class="text">'+item.name+'</p>' +
                            '</li>'
                        })
                        $('.customers').html(customers)
                    } else {
                        alert('API：' + data.message)
                    }
                    toast.clear()
                },
                error: function(err) {
                    alert('系统错误')
                }
            })

            $('.customers').on('click', '.user-cell', function() {
                // alert(callback)
                userId = $(this).data('userid')

                if (callback.indexOf('userid') != -1) {
                    callback = replaceQueryString(callback, 'userid', userId)
                } else {
                    const dot = (callback.indexOf('?') != -1) ? '&' : '?'
                    callback = callback + dot + 'userid=' + userId + '&staff_id=' + staffId + '&staff_name=' + staffName
                }

                // alert(callback)
                window.location.href = callback
            })
        })

    </script>
</html>
